﻿using System;
using System.Data;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using System.Web;
using System.Web.Caching;


using LMD.Constant;
using LMD.Lib.Oracle;

namespace Cached
{
    /// <summary>
    /// Summary description for ICPMenu
    /// </summary>
    public class ICPMenu : CacheAbstraction
    {
        public DataTable GetMenuList()
        {
            return GetMenuList(false);
        }
        public DataTable GetMenuList(bool bForceRefresh)
        {
            String sxCacheKey = KCache.ICP_MENU;

            if(bForceRefresh)
            {
                HttpRuntime.Cache.Remove(sxCacheKey);
            }

            if (HttpRuntime.Cache[sxCacheKey] == null)
            {
                DBLib dbl = new DBLib();

                String sxQuery =
                @"
                select
                    PAGE_NAME, 
                    P_MENU_GROUP_ID, 
                    GROUP_TITLE, 
                    GROUPING_INDEX, 
                    ITEM_TITLE, 
                    URL, 
                    nvl(USER_GROUP, 'ALL') USER_GROUP
                from
                    V_ICP_MENU
                order by
                    GROUP_TITLE asc, GROUPING_INDEX asc";

                DataTable dt = dbl.GetDataTable(sxQuery);

                AddToCache(sxCacheKey, dt, CacheType.Daily);
                
            }
            return (DataTable)HttpRuntime.Cache[sxCacheKey];
        }        

        public DataTable GetMenuList(String sxPageName)
        {
            return GetMenuList(sxPageName, false);
        }
        public DataTable GetMenuList(String sxPageName, bool bForceRefresh)
        {
            DataTable dt = GetMenuList(bForceRefresh);

            String sxFilter = String.Format("PAGE_NAME = '{0}' ", sxPageName);

            if(!ICPUser.IsAdmin)
            {
                sxFilter += String.Format(" and ( USER_GROUP = 'ALL' OR USER_GROUP = '{0}' ) ", ICPUser.Role);
            }

            return new DataView(dt,
                sxFilter, 
                "GROUP_TITLE asc, GROUPING_INDEX asc", 
                DataViewRowState.CurrentRows).ToTable();
        }

        public override String GetCacheFilename()
        {
            return KCache.Filename.MENU_LIST;
        }
    }
}